package org.example.domain.activity_manage.service.Impl;

import org.example.domain.activity_manage.model.entity.ActivityStrategyPrizeEntity;
import org.example.domain.activity_manage.model.entity.PrizeEntity;
import org.example.domain.activity_manage.service.IActivityService;
import org.example.domain.activity_manage.service.IActivityStrategyPrizeService;
import org.example.domain.activity_manage.service.IPreloadService;
import org.example.domain.activity_manage.service.IPrizeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lxc18
 * @date 2025/3/29 11:47
 * @description IPreloadServiceImpl
 */
@Service
public class IPreloadServiceImpl implements IPreloadService {
    @Resource
    private IActivityService activityService;
    @Resource
    private IActivityStrategyPrizeService activityStrategyPrizeService;

    @Resource
    private IPrizeService prizeService;


    @Override
    public void preload(Long activityId) {
        //查询活动信息
        activityService.queryActivityById(activityId);
        //查询策略信息
        List<ActivityStrategyPrizeEntity> activityStrategyPrizeEntities = activityStrategyPrizeService.queryStrategyByActivityId(activityId);
        for (ActivityStrategyPrizeEntity activityStrategyPrizeEntity : activityStrategyPrizeEntities) {
            //对奖品id进行处理，将逗号分隔的字符串转换为List<Long>
            String[] prizeIds = activityStrategyPrizeEntity.getPrizeIds().split(",");
            for (String prizeId : prizeIds) {
                long parsed = Long.parseLong(prizeId);
                PrizeEntity prizeEntity = prizeService.queryPrizeById(parsed);
                if(prizeEntity != null){
                    //已用库存和剩余库存信息
                    prizeService.queryPrizeStock(parsed);
                    prizeService.queryPrizeUsedStock(parsed);
                }
            }
        }
    }
}
